package com.xpgk.beetl.tag;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.beetl.core.Tag;

import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;
import com.xpgk.constant.ConstantRender;
import com.xpgk.mvc.codeSet.Code;
import com.xpgk.mvc.productSet.Product;
import com.xpgk.tools.ToolSqlXml;

/**
 * 参数输出标签：支持select、radio、checkbox
 * 
 */
public class TreatmentTag extends Tag {

	private static final Log log = Log.getLog(TreatmentTag.class);

	@SuppressWarnings("unchecked")
	@Override
	public void render() {
		try {
			Map<String, String> param = (Map<String, String>) args[1];

			if(log.isDebugEnabled()) log.debug("获取标签属性");

			String id = param.get("id") == null ? "" : param.get("id");
			String name = param.get("name") == null ? "" : param.get("name");
			
			String class_ = param.get("class") == null ? "" : param.get("class");
			String style = param.get("style") == null ? "" : param.get("style");
			String dataOptions = param.get("dataOptions") == null ? "" : param.get("dataOptions");

			ctx.byteWriter.writeString(select(id, name, class_, style, dataOptions));
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 下拉标签 value=ids
	 * @param id
	 * @param name
	 * @param class_
	 * @param style
	 * @param number
	 * @param defaultnumber
	 * @return
	 */
	private String select(String id, String name, String class_, String style, String data_options){
		StringBuilder sb = new StringBuilder();
		if (StrKit.notBlank(id)) {
			sb.append("<select id=\"").append(id).append("\" name=\"").append(name);
			sb.append("\" class=\"").append(class_).append("\" style=\"").append(style).append("\" data-options=\"").append(data_options).append("\" >");
		} else {
			sb.append("<select name=\"").append(name).append("\" class=\"").append(class_);
			sb.append("\" style=\"").append(style).append("\" data-options=\"").append(data_options).append("\" >");
		}
		sb.append("<option value='-1'>-请选择-</option>");
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("column", Product.column_typeIds);
		String sql = ToolSqlXml.getSqlMy(Product.sqlId_column, param, ConstantRender.sql_renderType_beetl);
		List<Product> lcs = Product.dao.find(sql, Code.lc_ids);
		
		for (Product lc : lcs) {
			String namesTemp = lc.getStr(Product.column_name);
			String ids = lc.getPKValue();
			sb.append("<option value=\"").append(ids).append("\">");
			sb.append(namesTemp);
			sb.append("</option>");
		}
		sb.append("</select>");
		
		return sb.toString();
	}
}
